package com.leetcode.partition1;

/**
 * @author `RKC`
 * @date 2021/12/5 9:18
 */
public class LC50快速幂 {

    public static double myPow(double x, int n) {
        if (x == 2 && n == -2147483648) return 0;
        return n < 0 ? 1 / quickPow(x, -n) : quickPow(x, n);
    }

    private static double quickPow(double x, int n) {
        double res = 1.0;
        while (n > 0) {
            if ((n & 1) == 1) res = res * x;
            n >>= 1;
            x = x * x;
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(myPow(2, 2147483647));
    }
}
